package fr.snac.notification;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimerTask;

import fr.snac.serialization.Infos;
import fr.snac.serialization.InfosDeserializer;
import fr.snac.utils.DateUtil;
import fr.snac.utils.FilesUtil;

public class CheckTimerTask extends TimerTask{

	CheckService service;
	AsyncCheck asyncTask;

	public CheckTimerTask(CheckService service){
		this.service = service;		
	}

	@Override
	public void run() {


		Calendar cal = GregorianCalendar.getInstance();
		String dateString = DateUtil.getUSDate(cal);


		if (FilesUtil.isInfosFileCreated()){
			InfosDeserializer deSerializer = new InfosDeserializer();
			deSerializer.deSerialize();

			Infos infos = deSerializer.getInfos();

			if (infos.containsKey(dateString))
				return;	
		}

		int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);

		if (hourOfDay == 9){
			if (minute >= 50){
				asyncTask = new AsyncCheck(CheckTimerTask.this, service.getApplicationContext());
				asyncTask.execute(cal);
			}
		}else if (hourOfDay > 9){
			asyncTask = new AsyncCheck(CheckTimerTask.this, service.getApplicationContext());
			asyncTask.execute(cal);
		}


		/*if (hourOfDay >= 9){

				if (hourOfDay == 9 && minute >= 50){
					asyncTask = new AsyncCheck(CheckTimerTask.this, service.getApplicationContext());
					asyncTask.execute(cal);
				}else{
					asyncTask = new AsyncCheck(CheckTimerTask.this, service.getApplicationContext());
					asyncTask.execute(cal);
				}
			}*/

	}

}
